<template>
  <div class="mobile-container">
    <div class="app-content">
      <!-- 顶部栏 -->
      <div class="header">
        <div class="header-top">
          <div class="back-icon" @click="handleBack">
            <Icon icon="mdi:arrow-left"/>
          </div>
          <div class="page-title">个人资料</div>
          <div class="placeholder"></div>
        </div>
      </div>

      <!-- 资料列表 -->
      <div class="profile-list">
        <div class="settings-group">
          <!-- 头像 -->
          <div class="settings-item" @click="editAvatar">
            <div class="item-left">
              <span class="item-text">头像</span>
            </div>
            <div class="item-right">
              <img :src="userInfo.avatar" class="avatar-preview" alt="头像">
              <Icon icon="mdi:chevron-right" class="item-arrow" />
            </div>
          </div>

          <!-- 昵称 -->
          <div class="settings-item" @click="editNickname">
            <div class="item-left">
              <span class="item-text">昵称</span>
            </div>
            <div class="item-right">
              <span class="item-value">{{ userInfo.nickname }}</span>
              <Icon icon="mdi:chevron-right" class="item-arrow" />
            </div>
          </div>

          <!-- 手机号 -->
          <div class="settings-item">
            <div class="item-left">
              <span class="item-text">手机号</span>
            </div>
            <div class="item-right">
              <span class="item-value">{{ userInfo.phone }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 头像选择底部弹窗 -->
      <div v-if="showAvatarDialog" class="bottom-sheet-mask" @click="showAvatarDialog = false">
        <div class="bottom-sheet" @click.stop>
          <div class="sheet-header">
            <div class="sheet-handle"></div>
          </div>
          <div class="sheet-content">
            <div class="action-buttons">
              <div class="action-button" @click="chooseFromAlbum">
                <div class="action-icon">
                  <Icon icon="mdi:image" />
                </div>
                <span class="action-text">从相册选择</span>
              </div>
              <div class="action-button" @click="takePhoto">
                <div class="action-icon">
                  <Icon icon="mdi:camera" />
                </div>
                <span class="action-text">拍照</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 昵称编辑弹窗 -->
      <div v-if="showNicknameDialog" class="custom-dialog-mask">
        <div class="custom-dialog">
          <div class="dialog-header">
            <h3>编辑昵称</h3>
            <span class="close-btn" @click="showNicknameDialog = false">×</span>
          </div>
          <div class="dialog-body">
            <div class="input-group">
              <label class="input-label">昵称</label>
              <input
                v-model="tempNickname"
                type="text"
                class="custom-input"
                placeholder="请输入昵称"
                maxlength="20"
              >
              <div class="char-count">{{ tempNickname.length }}/20</div>
            </div>
            <button class="convert-confirm-btn" @click="saveNickname">
              保存
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue2'

export default {
  name: 'ProfilePage',
  components: {
    Icon
  },
  data() {
    return {
      userInfo: {
        avatar: require('../../../../assets/images/Logo.png'),
        nickname: '用户昵称',
        phone: '13778884844'
      },
      showAvatarDialog: false,
      showNicknameDialog: false,
      tempNickname: ''
    }
  },
  methods: {
    handleBack() {
      this.$router.go(-1)
    },

    editAvatar() {
      this.showAvatarDialog = true
    },

    editNickname() {
      this.tempNickname = this.userInfo.nickname
      this.showNicknameDialog = true
    },

    saveNickname() {
      if (!this.tempNickname.trim()) {
        alert('昵称不能为空')
        return
      }
      this.userInfo.nickname = this.tempNickname
      this.showNicknameDialog = false
      // TODO: 调用接口保存昵称
    },

    chooseFromAlbum() {
      const input = document.createElement('input')
      input.type = 'file'
      input.accept = 'image/*'
      input.onchange = (e) => {
        const file = e.target.files[0]
        if (file) {
          this.uploadAvatar(file)
        }
      }
      input.click()
      this.showAvatarDialog = false
    },

    takePhoto() {
      alert('调用摄像头拍照功能')
      this.showAvatarDialog = false
    },

    uploadAvatar(file) {
      const reader = new FileReader()
      reader.onload = (e) => {
        this.userInfo.avatar = e.target.result
      }
      reader.readAsDataURL(file)
    }
  }
}
</script>

<style scoped>
.mobile-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 20px;
}

.app-content {
  width: 375px;
  min-height: 667px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

/* 顶部栏样式 */
.header {
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-icon {
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #333;
}

.back-icon:hover {
  background: #f5f5f5;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.placeholder {
  width: 36px; /* 与back-icon保持对称 */
}

/* 设置列表样式 */
.profile-list {
  flex: 1;
  padding: 20px;
}

.settings-group {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  margin-bottom: 20px;
  overflow: hidden;
}

.settings-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 1px solid #f8f9fa;
}

.settings-item:last-child {
  border-bottom: none;
}

.settings-item:hover {
  background: #f8f9fa;
}

.item-left {
  display: flex;
  align-items: center;
}

.item-text {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.item-arrow {
  color: #999;
  font-size: 18px;
}

.avatar-preview {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 8px;
}

.item-right {
  display: flex;
  align-items: center;
}

.item-value {
  font-size: 14px;
  color: #666;
  margin-right: 8px;
}

/* 底部弹窗样式 */
.bottom-sheet-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 1000;
  border-radius: 20px;
  animation: fadeIn 0.3s ease;
}

.bottom-sheet {
  width: 100%;
  background: white;
  border-radius: 20px 20px 0 0;
  padding: 0 20px 0;
  transform: translateY(0);
  animation: slideUp 0.3s ease;
  max-height: 80vh;
  overflow-y: auto;
}

.sheet-header {
  display: flex;
  justify-content: center;
  padding: 12px 0;
}

.sheet-handle {
  width: 40px;
  height: 4px;
  background: #e0e0e0;
  border-radius: 2px;
}

.sheet-content {
  padding: 10px 0;
}

.action-buttons {
  margin-bottom: 12px;
}

.action-button {
  display: flex;
  align-items: center;
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 12px;
}

.action-button:hover {
  background: #f8f9fa;
}

.action-icon {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  color: #333;
}

.action-icon svg {
  width: 20px;
  height: 20px;
}

.action-text {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* 昵称编辑弹窗样式保持原样 */
.custom-dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  border-radius: 20px;
}

.custom-dialog {
  width: 280px;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  background: #fafafa;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  font-size: 20px;
  color: #999;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.close-btn:hover {
  color: #666;
}

.dialog-body {
  padding: 20px;
}

.input-group {
  margin-bottom: 20px;
  position: relative;
}

.input-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: black;
}

.custom-input {
  width: 100%;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
  background: white;
}

.custom-input:focus {
  border-color: #1890ff;
  background: white;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.custom-input::placeholder {
  color: #999;
}

.char-count {
  position: absolute;
  right: 12px;
  bottom: -20px;
  font-size: 12px;
  color: #999;
}

.convert-confirm-btn {
  width: 100%;
  height: 44px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-top: 8px;
  transition: background-color 0.3s;
}

.convert-confirm-btn:hover {
  background: #40a9ff;
}

.convert-confirm-btn:active {
  background: #096dd9;
}

/* 响应式设计 */
@media (max-width: 375px) {
  .mobile-container {
    padding: 0;
  }

  .app-content {
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
  }
}
</style>
